/**
 * 
 */
package com.morgankelly.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.morgankelly.po.Account;
import com.morgankelly.po.Blog;
import com.morgankelly.po.BlogComment;
import com.morgankelly.po.Category;
import com.morgankelly.po.Tag;
import com.morgankelly.service.AuthenticationService;
import com.morgankelly.service.AuthenticationServiceImpl;
import com.morgankelly.service.BlogService;
import com.morgankelly.service.BlogServiceImpl;
import com.morgankelly.util.Constants;
import com.morgankelly.util.LinkEnum;
import com.morgankelly.util.MonthEnum;
import com.morgankelly.vo.ArchiveVO;
import com.morgankelly.vo.BlogCommentVO;
import com.morgankelly.vo.BlogVO;
import com.morgankelly.vo.CategorySideBarVO;
import com.morgankelly.vo.TagSideBarVO;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author morgan.liao
 *
 */
public class BaseAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	//Put all action results in base action. You also can find other results in 
    //class: com.opensymphony.xwork2.Action: SUCCESS, NONE, ERROR, INPUT, LOGIN.
    public static final String SUBMIT = "submit";
    public static final String DELETE = "delete";
    public static final String UPDATE = "update";
    public static final String ADD = "add";
    public static final String DISPLAY = "display";
    public static final String BACK = "back";
    public static final String LOGOUT = "logout";
    public static final String FAILED = "failed";
    public static final String UPLOAD = "upload";
    
    
    public static final String REGISTER_INPUT = "registerInput";
    public static final String REGISTER = "register";
    public static final String UPDATE_INPUT = "updateInput";
    

	/**
	 * This is default method. (non-Javadoc)
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	public String execute() {
		return SUCCESS;
	}
	
	private List<Category> categoryList;
	private List<Account> accountList;
	private List<BlogVO> blogVOList;
	private List<Blog> blogList;
	private List<CategorySideBarVO> categorySideBarVOList;
	private List<TagSideBarVO> tagSideBarVOList;
	
	private List<BlogComment> blogCommentList;
	private List<BlogCommentVO> blogCommentVOList;
	
	private Map<String, String> linkMap;
	private List<ArchiveVO> archiveVOList;
	
	private List<Tag> tagList;
	
	

	public List<TagSideBarVO> getTagSideBarVOList() {
		return tagSideBarVOList;
	}

	public void setTagSideBarVOList(List<TagSideBarVO> tagSideBarVOList) {
		this.tagSideBarVOList = tagSideBarVOList;
	}

	public List<Tag> getTagList() {
		return tagList;
	}

	public void setTagList(List<Tag> tagList) {
		this.tagList = tagList;
	}

	public List<ArchiveVO> getArchiveVOList() {
		return archiveVOList;
	}

	public void setArchiveVOList(List<ArchiveVO> archiveVOList) {
		this.archiveVOList = archiveVOList;
	}

	public Map<String, String> getLinkMap() {
		return linkMap;
	}

	public void setLinkMap(Map<String, String> linkMap) {
		this.linkMap = linkMap;
	}

	public List<Blog> getBlogList() {
		return blogList;
	}

	public void setBlogList(List<Blog> blogList) {
		this.blogList = blogList;
	}

	public List<BlogCommentVO> getBlogCommentVOList() {
		return blogCommentVOList;
	}

	public void setBlogCommentVOList(List<BlogCommentVO> blogCommentVOList) {
		this.blogCommentVOList = blogCommentVOList;
	}

	public List<BlogComment> getBlogCommentList() {
		return blogCommentList;
	}

	public void setBlogCommentList(List<BlogComment> blogCommentList) {
		this.blogCommentList = blogCommentList;
	}

	public List<BlogVO> getBlogVOList() {
		return blogVOList;
	}

	public void setBlogVOList(List<BlogVO> blogVOList) {
		this.blogVOList = blogVOList;
	}

	public List<Account> getAccountList() {
		return accountList;
	}

	public void setAccountList(List<Account> accountList) {
		this.accountList = accountList;
	}
	

	public List<CategorySideBarVO> getCategorySideBarVOList() {
		return categorySideBarVOList;
	}

	public void setCategorySideBarVOList(
			List<CategorySideBarVO> categorySideBarVOList) {
		this.categorySideBarVOList = categorySideBarVOList;
	}

	public List<Category> getCategoryList() {
		return categoryList;
	}

	public void setCategoryList(List<Category> categoryList) {
		this.categoryList = categoryList;
	}
	
	protected void initSideBar() {
		
		//Init CategorySideBarVO List
		categorySideBarVOList = new ArrayList<CategorySideBarVO>();
		CategorySideBarVO categorySideBarVO;
		
		for (Category category : getBlogService().getCategoryList()) {
			categorySideBarVO = new CategorySideBarVO();
			
			Long categoryID = category.getKey().getId();
			categorySideBarVO.setId(categoryID);
			categorySideBarVO.setName(category.getName());
			categorySideBarVO.setBlogNum(getBlogService().getBlogNumByCategoryID(categoryID));
			
			categorySideBarVOList.add(categorySideBarVO);
		}
		
		//Init link enum
		linkMap = new HashMap<String, String>();
        for (LinkEnum link : LinkEnum.values()) {
        	linkMap.put(link.linkName(), link.linkURL());
        }
        
		//Init archive enum
        archiveVOList = new ArrayList<ArchiveVO>();
        ArchiveVO archiveVO;

		Calendar current = Calendar.getInstance();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(Constants.FORMAT_TIME_7);

		String yearMonth = simpleDateFormat.format(current.getTime());
        for (int i=0;i< Constants.SIDE_BAR_TAB_LENGTH - 1; i++) {
        	
        	archiveVO = new ArchiveVO();
        	
        	archiveVO.setMonthURL(yearMonth);
        	String[] times = yearMonth.split("-");
        	archiveVO.setMonthName(MonthEnum.englishName(times[1]) + " " + times[0]);
        	archiveVO.setCommentNum(33); //TODO:
        	
        	archiveVOList.add(archiveVO);
        	
			current.add(Calendar.MONTH, -1);
			yearMonth = simpleDateFormat.format(current.getTime());
        }
        
        //Init Tag List
		tagSideBarVOList = new ArrayList<TagSideBarVO>();
		TagSideBarVO tagSideBarVO;
		
		for (Tag tag : getBlogService().getTagList()) {
			tagSideBarVO = new TagSideBarVO();
			
			Long tagID = tag.getKey().getId();
			tagSideBarVO.setId(tagID);
			tagSideBarVO.setName(tag.getName());
			
			int fontSize = 8 + tag.getNumInBlog();
			tagSideBarVO.setCssStyle("font-size:" + fontSize + "pt");
			tagSideBarVO.setNumInBlog(tag.getNumInBlog());
			
			tagSideBarVOList.add(tagSideBarVO);
		}
	}
	
	protected List<BlogVO> initBlogVOBySearchKey(String searchKey) {
		
		List<BlogVO> blogVOList = new ArrayList<BlogVO>();
		
		List<Blog> blogList;
		if(searchKey != null && !searchKey.equals("")) {
			blogList = getBlogService().getBlogListBySearchKey(searchKey);
		} else {
			blogList = getBlogService().getBlogList();
		}

		
		BlogVO blogVO;

		for (Blog blog : blogList) {
			blogVO = new BlogVO();
			blogVO.setId(blog.getKey().getId());
			blogVO.setTitle(blog.getTitle());
			blogVO.setContent(blog.getContent());

			Account account = getAuthService().getAccountByID(blog.getAccountKey().getId());
		    blogVO.setAccountDisplayName(account.getDisplayName());
		    blogVO.setAccountPicPath(account.getPicPath());
		    
		    
			Category category = getBlogService().getCategoryByID(blog.getCategoryKey().getId());
		    blogVO.setCategoryName(category.getName());
		    
		    blogVO.setCreateTime(blog.getCreateTime());
		    
			int commentNum = getBlogService().getCommentNumByBlogID(blog.getKey().getId());
			
		    blogVO.setCommentNum(commentNum);
		    
		    blogVOList.add(blogVO);
		}
		
		return blogVOList;
	}
	
    private AuthenticationService authService = new AuthenticationServiceImpl();
    private BlogService blogService = new BlogServiceImpl();


	public AuthenticationService getAuthService() {
		return authService;
	}

	public void setAuthService(AuthenticationService authService) {
		this.authService = authService;
	}

	public BlogService getBlogService() {
		return blogService;
	}

	public void setBlogService(BlogService blogService) {
		this.blogService = blogService;
	}
}
